/*
 * Copyright (C) 2017 Jared Rummler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jaredrummler.ohos.colorpicker.demo;

import com.jaredrummler.ohos.colorpicker.*;
import com.jaredrummler.ohos.colorpicker.utils.Log;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.net.Uri;

import static com.jaredrummler.ohos.colorpicker.ColorPickerDialog.TYPE_PRESETS;

public class MainActivity extends FractionAbility implements ColorPickerDialogListener {

    private static final String TAG = "MainActivity";

    private ColorPickerDialog dialog;

    // Give your color picker dialog unique IDs if you have multiple dialogs.
    private static final int DIALOG_ID = 0;

    int color1 = 0xFF3F51B5, color2 = 0xFF009688, color3 = 0xFFEC407A, color4 = 0x890099CC, color5 = 0xFFCDDC39, color6;

    int[] custom_preset = new int[]{
            0xfff44336, 0xffe91e63, 0xff9c27b0, 0xff673ab7, 0xff3f51b5,
            0xff2196f3, 0xff03a9f4, 0xff00bcd4, 0xff009688, 0xff4caf50,
            0xff8bc34a, 0xffcddc39, 0xffffeb3b, 0xffffc107, 0xffff9800,
            0xffff5722, 0xff795548, 0xff9e9e9e, 0xff607d8b, 0xffffffff};

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
//    if (savedInstanceState == null) {
//      getSupportFragmentManager().beginTransaction().add(android.R.id.content, new DemoFragment()).commit();
//        ColorPreferenceCompat cv = (ColorPreferenceCompat) findComponentById(ResourceTable.Id_button1);
//        cv.setAbility(this);
        initTitleBar();
//    }
        ColorPanelView view1 = (ColorPanelView) findComponentById(ResourceTable.Id_cpv_color_panel_view1);
        view1.setColor(color1);
        Component c1 = (Component) view1.getComponentParent();
        c1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ColorPickerDialog dialog = ColorPickerDialog.newBuilder()
                        .setColor(color1)
                        .create(getContext());
                dialog.setColorPickerDialogListener(new ColorPickerDialogListener() {
                    @Override
                    public void onColorSelected(int dialogId, int color) {
                        color1 = color;
                        view1.setColor(color1);
                    }

                    @Override
                    public void onDialogDismissed(int dialogId) {

                    }
                });
                dialog.show();
            }
        });
        ColorPanelView view2 = (ColorPanelView) findComponentById(ResourceTable.Id_cpv_color_panel_view2);
        view2.setColor(color2);
        ((Component) view2.getComponentParent()).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ColorPickerDialog dialog = ColorPickerDialog.newBuilder()
                        .setColor(color2)
                        .setColorShape(ColorShape.SQUARE)
                        .create(getContext());
                dialog.setColorPickerDialogListener(new ColorPickerDialogListener() {
                    @Override
                    public void onColorSelected(int dialogId, int color) {
                        color2 = color;
                        view2.setColor(color2);
                    }

                    @Override
                    public void onDialogDismissed(int dialogId) {

                    }
                });
                dialog.show();
            }
        });
        ColorPanelView view3 = (ColorPanelView) findComponentById(ResourceTable.Id_cpv_color_panel_view3);
        view3.setColor(color3);
        ((Component) view3.getComponentParent()).setClickedListener((view) -> {
            ColorPickerDialog dialog = ColorPickerDialog.newBuilder()
                    .setColor(color3)
                    .setAllowPresets(false)
                    .setDialogTitle(ResourceTable.String_app_name)
                    .setDialogType(ColorPickerDialog.TYPE_CUSTOM)
                    .create(getContext());
            dialog.setColorPickerDialogListener(new ColorPickerDialogListener() {
                @Override
                public void onColorSelected(int dialogId, int color) {
                    color3 = color;
                    view3.setColor(color3);
                }

                @Override
                public void onDialogDismissed(int dialogId) {

                }
            });
            dialog.show();
        });
        ColorPanelView view4 = (ColorPanelView) findComponentById(ResourceTable.Id_cpv_color_panel_view4);
        view4.setColor(color4);
        ((Component) view4.getComponentParent()).setClickedListener((view) -> {
            ColorPickerDialog dialog = ColorPickerDialog.newBuilder()
                    .setColor(color4)
                    .setShowAlphaSlider(true)
                    .setShowColorShades(false)
                    .create(getContext());
            dialog.setColorPickerDialogListener(new ColorPickerDialogListener() {
                @Override
                public void onColorSelected(int dialogId, int color) {
                    color4 = color;
                    view4.setColor(color4);
                }

                @Override
                public void onDialogDismissed(int dialogId) {

                }
            });
            dialog.show();
        });
        ColorPanelView view5 = (ColorPanelView) findComponentById(ResourceTable.Id_cpv_color_panel_view5);
        view5.setColor(color5);
        ((Component) view5.getComponentParent()).setClickedListener((view) -> {
            ColorPickerDialog dialog = ColorPickerDialog.newBuilder()
                    .setColor(color5)
                    .setAllowCustom(false)
                    .setShowColorShades(false)
                    .setPresets(custom_preset)
                    .create(getContext());
            dialog.setColorPickerDialogListener(new ColorPickerDialogListener() {
                @Override
                public void onColorSelected(int dialogId, int color) {
                    color5 = color;
                    view5.setColor(color5);
                }

                @Override
                public void onDialogDismissed(int dialogId) {

                }
            });
            dialog.show();
        });
        Component view6 = (Component) findComponentById(ResourceTable.Id_button_last);
        view6.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.jaredrummler.ohos.colorpicker.demo")
                        .withAbilityName("com.jaredrummler.ohos.colorpicker.demo.ColorPickerActivity")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });
    }

    public boolean initTitleBar() {
        Text title = (Text) findComponentById(ResourceTable.Id_titleBarTitle);
        title.setText(ResourceTable.String_app_name);
        Button dialogButton = (Button) findComponentById(ResourceTable.Id_titleBarDialog);
        dialogButton.setClickedListener((view) -> {
            dialog = ColorPickerDialog.newBuilder()
                    .setDialogType(ColorPickerDialog.TYPE_CUSTOM)
                    .setAllowPresets(false)
                    .setDialogId(DIALOG_ID)
                    .setColor(0xFF000000)
                    .setShowAlphaSlider(true)
                    .show(this);
        });
        return true;
    }

    @Override
    public void onColorSelected(int dialogId, int color) {
        Log.d(TAG, "onColorSelected() called with: dialogId = [" + dialogId + "], color = [" + color + "]");
        switch (dialogId) {
            case DIALOG_ID:
                // We got result from the dialog that is shown when clicking on the icon in the action bar.
                new ToastDialog(MainActivity.this).setText("Selected Color: #" + Integer.toHexString(color)).show();
                break;
        }
    }

    @Override
    public void onDialogDismissed(int dialogId) {
        Log.d(TAG, "onDialogDismissed() called with: dialogId = [" + dialogId + "]");
    }

}
 